
#include "../ui.h"
//#include "../misc/app_includes.h"
#include "../app_includes.h"

#define WAVE_POINT_NUM      SIGNAL_WAVE_SIZE
#define WAVE_VALUE_MAX      (256)

static lv_obj_t *ui_screen;
static lv_obj_t *ui_Chart1;
static lv_obj_t *ui_Panel2;
static lv_obj_t *ui_Dropdown1;
static lv_obj_t *ui_Label1;
static lv_obj_t *ui_Editbox1;
static lv_obj_t *ui_Label2;
static lv_obj_t *ui_Editbox2;
static lv_obj_t *ui_Button1;
static lv_obj_t *ui_Label3;
static lv_obj_t *ui_keyboard;

static lv_chart_series_t *ui_series_wave;

static void wave_refresh(lv_event_t *e);
static void ta_event_cb(lv_event_t * e);
static void btn_event_click(lv_event_t * e);

//app应用入口
lv_obj_t *lv_signal_open(void)
{
    ui_screen = lv_screen_create(ui_main_screen.desktop, LV_HOR_RES, LV_VER_RES);
    //ui_screen = ui_main_screen.desktop;
    ui_keyboard = lv_keyboard_create(ui_main_screen.desktop);
    lv_obj_add_flag(ui_keyboard, LV_OBJ_FLAG_HIDDEN);
    lv_obj_set_size(ui_keyboard,  LV_HOR_RES, LV_VER_RES / 3);
    lv_keyboard_set_mode(ui_keyboard, LV_KEYBOARD_MODE_NUMBER);
    //lv_obj_set_style_bg_opa(ui_keyboard, LV_OPA_30, LV_PART_MAIN|LV_STATE_DEFAULT);

    ui_Chart1 = lv_chart_create(ui_screen);
    lv_obj_set_height(ui_Chart1, 280);
    lv_obj_set_width(ui_Chart1, LV_VER_RES);
    lv_obj_align(ui_Chart1, LV_ALIGN_TOP_MID,0, UI_STATUS_BAR_HEIGHT);
    lv_chart_set_type(ui_Chart1, LV_CHART_TYPE_LINE);
    lv_chart_set_point_count(ui_Chart1,WAVE_POINT_NUM*4);//图表上点的个数
    lv_chart_set_range(ui_Chart1, LV_CHART_AXIS_PRIMARY_Y, 0, WAVE_VALUE_MAX);
    lv_chart_set_div_line_count(ui_Chart1, 9, 15);
    lv_chart_set_axis_tick(ui_Chart1, LV_CHART_AXIS_PRIMARY_X, 8, 4, 0, 5, false, 50);
    lv_chart_set_axis_tick(ui_Chart1, LV_CHART_AXIS_PRIMARY_Y, 8, 4, 0, 5, false, 50);
    lv_chart_set_axis_tick(ui_Chart1, LV_CHART_AXIS_SECONDARY_Y, 10, 5, 0, 0, false, 25);
    ui_series_wave = lv_chart_add_series(ui_Chart1, lv_color_hex(0x808080), LV_CHART_AXIS_PRIMARY_Y);
    lv_obj_set_style_bg_color(ui_Chart1, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_Chart1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_Chart1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_size(ui_Chart1, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);

    ui_Panel2 = lv_obj_create(ui_screen);
    lv_obj_set_width(ui_Panel2, LV_VER_RES);
    lv_obj_set_height(ui_Panel2, 130);
    lv_obj_align(ui_Panel2, LV_ALIGN_BOTTOM_MID,0,0);
    //lv_obj_align_to(ui_Panel2, ui_Chart1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
    lv_obj_clear_flag(ui_Panel2, LV_OBJ_FLAG_SCROLLABLE);      /// Flags

    ui_Dropdown1 = lv_dropdown_create(ui_Panel2);
    lv_obj_set_width(ui_Dropdown1, 150);
    lv_obj_set_height(ui_Dropdown1, 40);
    lv_dropdown_set_options_static(ui_Dropdown1, "正弦波\n矩形波\n三角波\n锯齿波");
    lv_obj_align(ui_Dropdown1, LV_ALIGN_TOP_LEFT,0,0);
    lv_obj_add_flag(ui_Dropdown1, LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
    lv_obj_set_style_text_font(ui_Dropdown1, &ui_font_FontChinese, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_Dropdown1, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_Dropdown1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_Dropdown1, &lv_font_montserrat_22, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(lv_dropdown_get_list(ui_Dropdown1), &ui_font_FontChinese,  LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_add_event_cb(ui_Dropdown1, wave_refresh, LV_EVENT_VALUE_CHANGED, NULL);

    ui_Editbox1 = lv_textarea_create(ui_Panel2);
    lv_obj_set_width(ui_Editbox1, 100);
    lv_obj_set_height(ui_Editbox1, 40);
    lv_obj_align(ui_Editbox1, LV_ALIGN_TOP_RIGHT,0, 0);
    lv_textarea_set_max_length(ui_Editbox1, 3);
    lv_textarea_set_accepted_chars(ui_Editbox1,"0123456789");
    lv_textarea_set_text(ui_Editbox1, "1");
    lv_obj_add_event_cb(ui_Editbox1, ta_event_cb, LV_EVENT_ALL, ui_keyboard);
    lv_obj_set_style_bg_color(ui_Editbox1, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_Editbox1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_Editbox1, LV_TEXT_ALIGN_LEFT, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_Editbox1, &lv_font_montserrat_22, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_Label1 = lv_label_create(ui_Panel2);
    lv_obj_set_width(ui_Label1, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_Label1, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_Label1, 59);
    lv_obj_set_y(ui_Label1, -35);
    lv_obj_align_to(ui_Label1, ui_Editbox1, LV_ALIGN_OUT_LEFT_MID, -90, -4);
    lv_label_set_text(ui_Label1, "频率[KHz]");
    lv_obj_set_style_text_font(ui_Label1, &ui_font_FontChinese, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_Editbox2 = lv_textarea_create(ui_Panel2);
    lv_obj_set_width(ui_Editbox2, 100);
    lv_obj_set_height(ui_Editbox2, 40);
    lv_obj_align_to(ui_Editbox2, ui_Editbox1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_textarea_set_max_length(ui_Editbox2, 4);
    lv_textarea_set_accepted_chars(ui_Editbox2,"0123456789");
    lv_textarea_set_text(ui_Editbox2, "1000");
    lv_obj_add_event_cb(ui_Editbox2, ta_event_cb, LV_EVENT_ALL, ui_keyboard);
    lv_obj_set_style_bg_color(ui_Editbox2, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_Editbox2, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_Editbox2, LV_TEXT_ALIGN_LEFT, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_Editbox2, &lv_font_montserrat_22, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_Label2 = lv_label_create(ui_Panel2);
    lv_obj_set_width(ui_Label2, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_Label2, LV_SIZE_CONTENT);    /// 1
    lv_obj_align_to(ui_Label2, ui_Editbox2, LV_ALIGN_OUT_LEFT_MID, -90, -4);
    lv_label_set_text(ui_Label2, "峰峰值[mV]");
    lv_obj_set_style_text_font(ui_Label2, &ui_font_FontChinese, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_Button1 = lv_btn_create(ui_Panel2);
    lv_obj_set_width(ui_Button1, 150);
    lv_obj_set_height(ui_Button1, 40);
    lv_obj_align_to(ui_Button1, ui_Dropdown1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_add_flag(ui_Button1, LV_OBJ_FLAG_CHECKABLE|LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
    lv_obj_clear_flag(ui_Button1, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_radius(ui_Button1, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_Button1, lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_Button1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_add_event_cb(ui_Button1, btn_event_click, LV_EVENT_CLICKED, NULL);

    ui_Label3 = lv_label_create(ui_Button1);
    lv_obj_set_width(ui_Label3, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_Label3, LV_SIZE_CONTENT);    /// 1
    lv_obj_align(ui_Label3, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(ui_Label3, "启动");
    lv_obj_set_style_text_font(ui_Label3, &ui_font_FontChinese, LV_PART_MAIN | LV_STATE_DEFAULT);

    wave_refresh(NULL);

    return ui_screen;
}

static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    lv_obj_t * kb = lv_event_get_user_data(e);
    if(code == LV_EVENT_FOCUSED) {
        if(lv_indev_get_type(lv_indev_get_act()) != LV_INDEV_TYPE_KEYPAD) {
            lv_keyboard_set_textarea(kb, ta);
            lv_obj_set_style_max_height(kb, LV_HOR_RES * 2 / 3, 0);
            lv_obj_update_layout(ui_screen);   /*Be sure the sizes are recalculated*/

            lv_obj_align(ui_screen, LV_ALIGN_CENTER, 0, -lv_obj_get_height(kb));//上移kb高度
            //lv_obj_set_height(ui_screen, LV_VER_RES - lv_obj_get_height(kb));
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
            lv_obj_scroll_to_view_recursive(ta, LV_ANIM_OFF);
        }
    }
    else if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_align(ui_screen, LV_ALIGN_CENTER,0,0);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_indev_reset(NULL, ta);

    }
    else if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL) {
        lv_obj_align(ui_screen, LV_ALIGN_CENTER,0,0);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_state(ta, LV_STATE_FOCUSED);
        lv_indev_reset(NULL, ta);   /*To forget the last clicked object to make it focusable again*/
    }
}

static void wave_refresh(lv_event_t *e)
{
    uint16_t i,j;
    uint16_t index = lv_dropdown_get_selected(ui_Dropdown1);
    for(j=0;j!=4;j++) //画4遍
        for(i=0; i!=WAVE_POINT_NUM; i++) {
            lv_chart_set_next_value(ui_Chart1, ui_series_wave, wav_data[index][i]);
        }
    signal_out.type = index;
}

static void btn_event_click(lv_event_t * e)
{
    const char *txt;
    txt = lv_textarea_get_text(ui_Editbox1);
    signal_out.freq = str_to_uint(txt);
    txt = lv_textarea_get_text(ui_Editbox2);
    signal_out.vpp = str_to_uint(txt);
    signal_out.enable = lv_obj_has_state(ui_Button1, LV_STATE_CHECKED);
    signal_out.offset = 1650; //默认
    signal_out_wave(&signal_out);
    /*
    if(signal_out.enable) {
        lv_obj_clear_flag(ui_Editbox1, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_clear_flag(ui_Editbox2, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_clear_flag(ui_Dropdown1, LV_OBJ_FLAG_CLICKABLE);
    }
    else {
        lv_obj_add_flag(ui_Editbox1, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_add_flag(ui_Editbox2, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_add_flag(ui_Dropdown1, LV_OBJ_FLAG_CLICKABLE);
    }
    */
}
